/*
 * station_pass_trainlist.c
 *
 *  Created on: 2013-2-19
 *      Author: panqiuhui
 */
#include <stdio.h>
#include <stdlib.h>
#include "../head/common_struct/station_pass_trainlist.h"

static void CopyToNodeStationPassTrain(StationPassTrainItem item,
		StationPassTrainNode *pnode);

void InitializeListStationPassTrain(StationPassTrainList * plist) {
	*plist = NULL;
}

bool ListIsEmptyStationPassTrain(const StationPassTrainList * plist) {
	if (*plist == NULL ) {
		return true;
	} else {
		return false;
	}
}

bool ListIsFullStationPassTrain(const StationPassTrainList * plist) {
	StationPassTrainNode * pt;
	bool full;

	pt = (StationPassTrainNode *) malloc(sizeof(StationPassTrainNode));
	if (pt == NULL ) {
		full = true;
	} else {
		full = false;
	}
	free(pt);
	return full;
}

unsigned int ListItemCountStationPassTrain(const StationPassTrainList * plist) {
	unsigned int count = 0;
	StationPassTrainNode * pnode = *plist;

	while (pnode != NULL ) {
		++count;
		pnode = pnode->next;
	}
	return count;
}

bool AddItemStationPassTrain(StationPassTrainItem item,
		StationPassTrainList *plist) {
	StationPassTrainNode *pnew;
	StationPassTrainNode *scan = *plist;

	pnew = (StationPassTrainNode *) malloc(sizeof(StationPassTrainNode));
	if (pnew == NULL ) {
		return false;
	}

	CopyToNodeStationPassTrain(item, pnew);
	pnew->next = NULL;
	if (scan == NULL ) {
		*plist = pnew;
	} else {
		while (scan->next != NULL ) {
			scan = scan->next;
		}
		scan->next = pnew;
	}
	return true;
}

void TraverseStationPassTrain(const StationPassTrainList * plist,
		void (*pfun)(StationPassTrainItem item)) {
	StationPassTrainNode * pnode = *plist;
	while (pnode != NULL ) {
		(*pfun)(pnode->item);
		pnode = pnode->next;
	}
}

void EmptyTheListStationPassTrain(StationPassTrainList * plist) {
	StationPassTrainNode * psave;
	while (*plist != NULL ) {
		psave = (*plist)->next;
		free(*plist);
		*plist = psave;
	}
}

static void CopyToNodeStationPassTrain(StationPassTrainItem item,
		StationPassTrainNode *pnode) {
	pnode->item = item;
}
